import { GameStorage, RankCloundData, StorageKey } from "./Global"
class LevelData {
    level: number
    lineWords: string
    codeLib: string
}
const _DATA: LevelData[] = [
    { level: 1, lineWords: '学而，时习之', codeLib: '而叐損首学习变时軟佋盒摺滿氻渤之' },
    { level: 2, lineWords: '满招损，谦受益', codeLib: '滿满溓挸谦妱抯益墆招抯愛受氻讼损' },
    { level: 3, lineWords: '知进退，守正道', codeLib: '退佋牊滿讫讫知道守正进氻哫氻妱首' },
    { level: 4, lineWords: '雄关漫道真如铁', codeLib: '雄軟关妱氻讼氻漫道孚佋真铁如监埙' },
    { level: 5, lineWords: '黄沙百战穿金甲', codeLib: '金滿甲孚首变損黄溓叐沙战埙摺百穿' },
    { level: 6, lineWords: '西北望，射天狼', codeLib: '变抯讫摺抯天抯佋射軟妱溓西挸氻渤首軟北狼盒溓渤望叐' },
    { level: 7, lineWords: '报与桃花一处开', codeLib: '讼花摺与报盒叐軟开渤羞桃讼一盒牊軟軟溓抯处盒讫摺佋' },
    { level: 8, lineWords: '男儿何不带吴钩', codeLib: '埙不讼孚墆监监带叐儿挸孚吴羞钩満男佋何墆谂愛妱埙盖' },
    { level: 9, lineWords: '锲而不舍，金石可镂', codeLib: '墆墆妱镂渤溓可金舍佋盒石盒孚埙不溓哫牊叓滿锲而溓监' },
    { level: 10, lineWords: '学而不厌，不亦乐乎', codeLib: '乐而埙羞叐軟損抯妱埙学变乎渤埙哫厌溓不亦谂渤墆羞不' },
    { level: 11, lineWords: '愚者千虑，必有一得', codeLib: '孚者墆首必佋哫摺氻牊孚讼叓佋渤挸变千一虑孚牊渤讼損盖叐得墆牊愚盒有首抯讼' },
    { level: 12, lineWords: '老骥伏枥，志在千里', codeLib: '摺摺挸老盖伏盒谂妱墆牊埙谂羞叓在认満监佋讫盒叐孚枥骥志妱监谂軟里认千損羞' },
    { level: 13, lineWords: '东方欲晓，莫道君行早', codeLib: '认孚溓欲羞讼莫叓讫行首愛谂讼盖晓讫墆认讼墆孚盖埙孚认早道孚叓讼方君东埙认' },
    { level: 14, lineWords: '天行健，君子以自强不息', codeLib: '行挸埙叐墆天摺滿牊埙盒叓子自挸监君强健叓讫息首羞渤牊妱孚摺以牊牊不牊埙叐' },
    { level: 15, lineWords: '前事之不忘，后事之师也', codeLib: '后軟溓讼认不羞挸愛軟讫讫之叓摺摺认前谂佋満埙羞滿忘师也抯溓盖愛事事叓之溓' },
    { level: 16, lineWords: '君子坦荡荡，小人长戚戚', codeLib: '君軟讼荡軟損谂盒讫监佋小佋抯盖谂渤滿坦牊盒牊戚挸变人荡讼子哫监长佋抯佋戚' },
    { level: 17, lineWords: '积财千万，不如薄技在身', codeLib: '抯愛叓薄盒叐妱如不損千万氻财挸埙技羞滿讼身哫滿讼妱变抯积愛在谂墆认溓监溓' },
    { level: 18, lineWords: '会当凌绝顶，一览众山小', codeLib: '軟孚氻叓览羞叐会凌挸盖挸羞挸墆叓小墆孚損盖埙当埙軟山満満众妱顶損損一損孚讫氻妱认溓叐认渤变绝墆盖損' },
    { level: 19, lineWords: '已是悬崖百丈冰，犹有花枝俏', codeLib: '冰讼羞谂愛氻孚俏花监叓溓叓埙墆犹叐百有枝哫滿讼哫氻墆悬摺盒监盒是氻叐損盖讫愛摺崖佋丈渤已叓叓孚妱变' },
    { level: 20, lineWords: '路漫漫其修远兮，吾将上下而求索', codeLib: '満軟渤氻首墆讼其修孚妱认下而孚监哫盖认牊渤索漫叐认远哫叓将上佋路埙兮摺叐漫抯变満变佋摺溓变求吾損盒' },
    { level: 21, lineWords: '人间四月芳菲尽，山寺桃花始盛开', codeLib: '渤滿菲愛监监盖四溓摺摺盒溓花盖始尽谂渤变讫监山哫认人抯埙満抯月间羞滿桃盒埙寺摺埙讫損滿芳羞溓开盛讫' },
    { level: 22, lineWords: '劝君更尽一杯酒，西出阳关无故人', codeLib: '变埙首无西妱监讼盖尽出軟君羞妱溓盖満认損劝盖愛氻氻盖挸关挸溓酒更变盒叓人一牊氻妱愛监軟埙故妱哫阳杯' },
    { level: 23, lineWords: '长风破浪会有时，直挂云帆济沧海', codeLib: '叐破沧氻讫有墆直监讼长叐监风溓羞浪叐氻墆妱損讼哫挸会摺妱抯挂佋佋讼变滿摺监溓海氻济抯滿帆云叓軟时挸' },
    { level: 24, lineWords: '千磨万击还坚劲，任尔东西南北风', codeLib: '磨哫南盒盒北万讼尔叓渤击軟风讫孚抯叐抯东埙谂谂牊氻哫任氻溓溓氻佋坚渤叓西讫谂千讫溓劲谂渤盒氻还谂抯' },
    { level: 25, lineWords: '大鹏一日同风起，扶摇直上九万里', codeLib: '羞牊日孚扶监监讼变溓氻认叐摇直一羞挸里监摺风讼盒万监讼上愛佋盒滿起挸哫氻叓摺佋墆同渤愛鹏叓九満大摺' },
    { level: 26, lineWords: '振长策而御宇内，执敲扑而鞭笞天下', codeLib: '羞溓策笞扑内孚天愛宇而墆讫愛下渤叓变渤敲认挸軟妱讼牊満执盖讫而孚讼佋滿振变氻叐御抯溓监鞭长叐盖溓埙' },
    { level: 27, lineWords: '一言以蔽之，学者之道，无他，贵在思想。', codeLib: '満抯叐墆讫摺滿盒愛谂牊无抯盒軟者一学贵氻认妱谂損滿想墆哫之。变抯认挸孚盖思埙満蔽挸以之牊佋摺渤言谂认谂羞他渤谂氻哫羞埙滿哫在道盖' },
    { level: 28, lineWords: '一切有为法，如梦幻泡影，如露亦如电，应作如是观', codeLib: '如认満滿一是溓谂氻谂牊有満讫讫认叐观妱梦氻讫叐露法妱影渤讫溓挸应氻泡妱盖电盒亦如作羞首妱如愛首切滿愛监认埙幻谂牊讫孚为挸氻讼軟如' }
]
export class GameData {
    static lineWords = '' // '学而，时习之'
    static codeLib = '' // '雨时乏宀习学之対斈肉穴而旪爱旪學'
    static answerWords = ''

    static getAllLevel() {
        return _DATA.length
    }

    // 初始化游戏数据
    static initLevelData(level: number) {
        if (typeof level === "string") level = parseInt(level)
        const LevelData = _DATA[level - 1]
        GameData.lineWords = LevelData.lineWords
        GameData.codeLib = LevelData.codeLib
        GameData.answerWords = ''
        // 存储当前关卡
        GameStorage.setItem(StorageKey.LEVEL, level)
        // 存储最大关卡
        const maxLevel = GameStorage.getItem(StorageKey.MAX_LEVEL) || 0
        GameStorage.setItem(StorageKey.MAX_LEVEL, Math.max(maxLevel, level))

        // 上传排行榜数据
        RankCloundData.pushData()

    }
    // 获取文字库
    static getCodeLib() { return shuffleString(GameData.codeLib) }
    // 获取下一个字符
    static getNextCode() {
        return GameData.lineWords[GameData.answerWords.length]
    }
    // 是否答对了
    static isSuccess() {
        return GameData.answerWords === GameData.lineWords
    }

}



// Fisher-Yates 洗牌算法 打乱字符串
function shuffleString(str: string): string {
    // 把字符串转换为字符数组
    const charArray = str.split('');
    let length = charArray.length;

    // 运用 Fisher-Yates 洗牌算法
    while (length > 0) {
        // 选取一个随机索引
        const randomIndex = Math.floor(Math.random() * length);
        length--;

        // 交换当前元素和随机选取的元素
        [charArray[length], charArray[randomIndex]] = [charArray[randomIndex], charArray[length]];
    }

    // 将数组重新组合成字符串
    return charArray.join('');
}


export class GameTools {
    // 放大镜数量
    static searchCount: number = 1
    // 每关放大镜最大数量
    static getSearchMaxCount: number = 5
}